import ejs from 'ejs';
import fs from 'fs'
import  {marked}  from 'marked';
import browserSync from 'browser-sync'


let browser;

const server = ()=>{
    browser = browserSync.create()
    browser.init({
        server:{
            baseDir:'./',
            index:'index.html'
        }
    })
}
const init = (cb)=>{
    const template = fs.readFileSync('./readme.md', 'utf8');
    ejs.renderFile('./template.ejs', {
        content:marked.parse(template),
        title:'hello'
    },(err,data)=>{
        if(err){
            return err
        }else{
            fs.writeFileSync('./index.html', data)
            cb()
        }
    })
}
fs.watchFile('./readme.md',(curr,prev)=>{
    if(curr.mtime !== prev.mtime){
        init(()=>{
            browser.reload()
        })
    }
})
init(()=>{
    server()
})